/*************************************************
 *FileName:      notecase.js
 *Description:   我的账户.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.0006,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var page=1,page_num=8,isUp = true;;
    /*
     * 主程序
     */ 
     
    window.addEventListener('notecase', function(e){//执行刷新
        loder();
        list();
    });

    $(document).ready(function() 
    {
        //绑定多个tap跳转页面
        bindTaps(['recharge']);
        mui.plusReady(loder)
        mui.plusReady(list)
//      loder();
//      list()
    });
    /**
     * loder
     * 查询余额
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function loder(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/myBalance",
            dataType:'jsonp',
            data: { uid: uid,token :token},
            success: function(data) {
                if(data.error_code == 0){
                    $('.balance-num').html('<em class="money"></em><span class="balance">'+data.balance+'</span>');
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }
        });
    }
    /**
     * list
     * 记录
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function list(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/moneyRecords",
            dataType:'jsonp',
            data: { uid: uid,token :token,page:page,page_num:page_num},
            success: function(data) {
                if(data.error_code == 0){
                    if(data.data.length == page_num){
                        $('.mui-scroll').children('.ys-scroll-btm').html('上拉加载更多');
                        isUp = true;
                    }else{
                        $('.mui-scroll').children('.ys-scroll-btm').html('已经到底了');
                        isUp = false;
                    }
                    $('.mui-scroll').children('.ys-scroll-top').html('下拉刷新数据').attr('style', false);
                    var olist='';
                    for(var i=0 ;i<data.data.length ; i++){
                        var otime =timesub(data.data[i].time);
                        var omeny = parseFloat(data.data[i].expense) ?'red">-'+ data.data[i].expense : 'blue">+'+data.data[i].income;
                        olist+='<tr class="msg-item"><td class="item-left">'+data.data[i].des+'</td>'
                             +'<td class="item-middle">'+otime+'</td><td class="item-right '+omeny+'</td></tr>';
                    }
                    $('.main').append(olist);
                    page++;
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }
        });
    }
    common.scrollUpDown($('.mui-scroll'),function(index){
        if(index>0&&isUp){
            isUp = false;
            list();
        }else if(index<0){
            $('.main').html('');
            isUp = true;
            page = 1;
            list();
        }else if(index>0 && !isUp){
            $('.mui-scroll').children('.ys-scroll-btm').html('已经到底了');
        }
    });
    

//时间计算
    function timesub(time){
        var date = new Date(time * 1000);
        var year = date.getFullYear();
        var mou = date.getMonth() + 1;
        var day = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        hours<10 ? hours='0'+hours :null;
        minutes<10 ? minutes='0'+minutes :null;
        day<10 ? day='0'+day :null;
        mou<10 ? mou='0'+mou :null;
        var newtime = year + '/' + mou + '/' + day + ' ' + hours + ':' +minutes ;
        return newtime;
    }